app_Ctrl.controller('findCtrl', function($scope,Common,$state,$ionicModal,$translate,findModule,$timeout,mineModule) {
// 1.活动页面
  // 初始化设置上拉加载更多为有效
  $scope.moreData = true;
  //上拉加载没有更多数据的时候显示提示
  $scope.nomoreData = false;

 

  //查询活动列表(活动列表参数)
  $scope.param = {
    page_index: 1,
    page_count: 10,
    act_type: '277'
  };
  $scope.get_active_list = function(){
    // console.log("1.params:"+JSON.stringify($scope.param));
    findModule.get_active_list( $scope.param).then(function(res){
        // console.log("获取活动列表："+JSON.stringify( res));
        $scope.Images_IP = res.imageIp;
        $scope.data = res.activitiesList;
    })
  }
  $scope.get_active_list();//获取活动列表
  //上拉加载更多；
  $scope.loadMore = function () {
    $scope.param.page_index++;
    var timer = null;
    // console.log("params:"+JSON.stringify( $scope.param))
    findModule.get_active_list($scope.param).then(function (res) {
        $scope.res = res.activitiesList;
        if ( $scope.res) {
            if ( $scope.res.length !== 0) {
                for (var i = 0; i < $scope.res.length; i++) {
                    $scope.data.push($scope.res[i]);
                }
            }
            if ( $scope.res.length === 0) {
                $scope.moreData = false;
                $scope.nomoreData = true;
            }
            timer = $timeout(function () {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            }, 1500);
        }else{
            $scope.moreData = false;
            $scope.nomoreData = true;
            $scope.$broadcast('scroll.infiniteScrollComplete');
        }
    })
  };
  //下拉刷新方法
  $scope.doRefresh = function(){
    console.log("开始上拉刷新。。。") 
    // $scope.get_active_list();
  };

// 2.品牌目录(楼层店铺)
 //获取楼层
  // getFloorSteps
  $scope.getfloor = function(){
      findModule.get_floor("getFloorSteps").then(function(res){
          $scope.floors = res;
          $scope.i = $scope.floors[0].floor_id;//默认第一个楼层显示为活动状态；
          // console.log("楼层信息："+JSON.stringify(res)); //初始化获取默认在活动楼层的店铺列表；
          $scope.params = {shop_floor:$scope.i};
          findModule.get_brand_list("getShopAndFloorByShop_floor",$scope.params).then(function(res){
              
              $scope.floor = res.floorMap;
              $scope.shop_lists = res.shopList;
              for( var i = 0;i<$scope.shop_lists.length;i++){
                  if($scope.shop_lists[i].shop_home == "1"){
                      $scope.shop_lists[i].isRecommend = true;//显示推荐图标
                  }else{
                        $scope.shop_lists[i].isRecommend = false;//隐藏推荐图标；
                  }
              }
              // console.log("初始化显示第一楼的列表"+JSON.stringify($scope.shop_lists));
              $scope.IMG = res.imageIp;
          })
      })
  }
  $scope.getfloor();//初始化楼层
  //切换：按楼层/按品牌；// 初始化默认先显示按楼层
  $scope.is_floor = true;
  $scope.is_select = false;
  //切换显示：品类/楼层
  $scope.show_type = "按品类"
  $scope.show = function(){
      if($scope.show_type == "按品类"){
          $scope.show_type = "按楼层"
          $scope.is_floor = false;
          $scope.is_select = true;
          $scope.get_category();//获取品类列表
      }else{
          $scope.show_type = "按品类"
          $scope.is_floor = true;
          $scope.is_select = false;
          $scope.getfloor();//切换显示楼层相关数据
      }
      
  }
  //点击当前楼层显示活跃状态；并获取当前楼层的所有店铺；
  $scope.is_active = function(n){
      // n 为店铺的id
      $scope.i=n;
      // console.log("=================="+$scope.i);// 根据楼层获取商铺列表// getShopByShop_floor   getShopAndFloorByShop_floor// String shop_floor
      $scope.params = {shop_floor:$scope.i};
      findModule.get_brand_list("getShopAndFloorByShop_floor",$scope.params).then(function(res){
          // console.log("111111111111111111111111"+JSON.stringify(res));
          $scope.floor = res.floorMap;
          $scope.shop_lists = res.shopList;
          for( var i = 0;i<$scope.shop_lists.length;i++){
              if($scope.shop_lists[i].shop_home == "1"){
                  $scope.shop_lists[i].isRecommend = true;
              }else{
                      $scope.shop_lists[i].isRecommend = false;
              }
          }
          $scope.IMG = res.imageIp;
      })
  }

  //获取品类数据；获取商铺类别列表getShop_type 返回 zdxId,zdxName
  $scope.get_category = function(){
    var params = {};
    mineModule.http("getShop_type",params).then(function(res){
      $scope.shop_type = res;
      var selected = res[0].zdxId//初始化加载进来显示的店铺
      $scope.get_shop_list(selected);
    })
  }

  //按品类搜索店铺；根据类别获取商铺 getShopByShop_type 参数 shop_type 就是 zdxId
  $scope.get_shop_list = function(selected){
    console.log("选中的分类为select："+selected)
    var params = {
      shop_type:selected
    }
    mineModule.http("getShopByShop_type",params).then(function(res){
    //   console.log("获取分类列表："+JSON.stringify(res));
      $scope.floor.floor_title = res.type_name;
      $scope.floor.floor_num = ""//清除楼层显示
      $scope.shop_lists = res.shopList;
      for( var i = 0;i<$scope.shop_lists.length;i++){
          if($scope.shop_lists[i].shop_home == "1"){
              $scope.shop_lists[i].isRecommend = true;//显示推荐图标
          }else{
                $scope.shop_lists[i].isRecommend = false;//隐藏推荐图标；
          }
      }
      $scope.IMG = res.imageIp;
    })
  }

//3.商场服务
    // 获取预约服务类型
    // getApptype
    $scope.get_service = function(){
        $scope.params = { };
        findModule.get_brand_list("getApptype",$scope.params).then(function(res){
            $scope.service_logo_IP = res.imageIp;
            $scope.service_list = res.apptypeList;
        })
    }
    $scope.get_service(); 
    //获取预约电话
    // 获取电话
    // getTel
    // $scope.get_tel = function(){
    //     findModule.get_brand_list("getTel").then(function(res){
    //         console.log("电话"+res);
    //         $scope.service_tel = res;
    //     })
    // }
    // $scope.get_tel();

    //页面生命周期监听
    // $scope.$on('$ionicView.beforeEnter', function() {
    //     $scope.get_active_list();
    // })

})


// 商场公告
.controller('mall_noticeCtrl', function($ionicHistory,$scope,Common,$state,$ionicModal,$translate,findModule,$timeout) {
     $scope.goback=function(){
        $ionicHistory.goBack();
    }
    // 初始化设置上拉加载更多为有效
    $scope.moreData = true;
    //上拉加载没有更多数据的时候显示提示
    $scope.nomoreData = false;
    $scope.params = {
        page_index: 1,
        page_count: 10,
        info_type: '1'
    };
    $scope.get_lost_list = function(){
        findModule.get_brand_list("getInfobarList", $scope.params).then(function(res){
            $scope.data = res;
            if($scope.data.length == 0){
                $scope.nomoreData = true;
            }
            // console.log(JSON.stringify(res))
        })
    }
    $scope.get_lost_list();
    //上拉加载更多；
    $scope.loadMore = function () {
        // console.log("检测到上拉事件")
        $scope.params.page_index++;
        var timer = null;
        findModule.get_brand_list("getInfobarList", $scope.params).then(function (res) {
            $scope.res = res;
            if ( $scope.res) {
                if ( $scope.res.length !== 0) {
                    for (var i = 0; i < $scope.res.length; i++) {
                        $scope.data.push($scope.res[i]);
                    }
                }
                console.log("--------------------------------"+ $scope.data.length );
                if ( $scope.res.length === 0) {
                    $scope.moreData = false;
                    $scope.nomoreData = true;
                }
                timer = $timeout(function () {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                }, 1500);
            }else{
                $scope.moreData = false;
                $scope.nomoreData = true;
                $scope.$broadcast('scroll.infiniteScrollComplete');
            }
        })
    };
    //上拉刷新方法
    $scope.doRefresh = function(){
        // 初始化设置上拉加载更多为有效
        $scope.moreData = true;
        //上拉加载没有更多数据的时候显示提示
        $scope.nomoreData = false;
        $scope.params = {
            page_index: 1,
            page_count: 10,
            info_type: '1'
        };
        findModule.get_brand_list("getInfobarList", $scope.params).then(function(res){
            $scope.data = res;
            if($scope.data.length == 0){
                $scope.nomoreData = true;
            }
            $scope.$broadcast('scroll.refreshComplete');
        })
    };
})
//商场公告

//失物招领
.controller('lostCtrl', function( $ionicHistory,$scope,Common,$state,$ionicModal,$translate,findModule,$timeout) {
    $scope.goback=function(){
        $ionicHistory.goBack();
    }
    // 初始化设置上拉加载更多为有效
    $scope.moreData = true;
    //上拉加载没有更多数据的时候显示提示
    $scope.nomoreData = false;
    $scope.params = {
        page_index: 1,
        page_count: 10,
        info_type: '4'
    };
    $scope.get_lost_list = function(){
        findModule.get_brand_list("getInfobarList", $scope.params).then(function(res){
            $scope.data = res;
            if($scope.data.length == 0){
                $scope.nomoreData = true;
            }
            // console.log(JSON.stringify(res))
        })
    }
    $scope.get_lost_list();
    //上拉加载更多；
    $scope.loadMore = function () {
        // console.log("检测到上拉事件")
        $scope.params.page_index++;
        var timer = null;
        findModule.get_brand_list("getInfobarList", $scope.params).then(function (res) {
            $scope.res = res;
            if ( $scope.res) {
                if ( $scope.res.length !== 0) {
                    for (var i = 0; i < $scope.res.length; i++) {
                        $scope.data.push($scope.res[i]);
                    }
                }
                console.log("--------------------------------"+ $scope.data.length );
                if ( $scope.res.length === 0) {
                    $scope.moreData = false;
                    $scope.nomoreData = true;
                }
                timer = $timeout(function () {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                }, 1500);
            }else{
                $scope.moreData = false;
                $scope.nomoreData = true;
                $scope.$broadcast('scroll.infiniteScrollComplete');
            }
        })
    };
    //上拉刷新方法
    $scope.doRefresh = function(){
        // 初始化设置上拉加载更多为有效
        $scope.moreData = true;
        //上拉加载没有更多数据的时候显示提示
        $scope.nomoreData = false;
        $scope.params = {
            page_index: 1,
            page_count: 10,
            info_type: '4'
        };
        findModule.get_brand_list("getInfobarList", $scope.params).then(function(res){
            $scope.data = res;
            if($scope.data.length == 0){
                $scope.nomoreData = true;
            }
            $scope.$broadcast('scroll.refreshComplete');
        })
    };
})
//失物招领

// //发现-》活动详情
.controller('active_detailCtrl', function($scope,$state,$stateParams,$ionicHistory,homeModule){
  $scope.goback = function(){
    $ionicHistory.goBack();
  }
  //$stateParams 接收传递过来的参数；//   console.log("最新活动列表传过来的id："+JSON.stringify($stateParams));
  $scope.active_id = $stateParams.active_id;
  // 查询活动详情// getActivitiesDetail// int act_id
  homeModule.active_detail("getActivitiesDetail",$scope.active_id ).then(function(res){
    console.log("huodong详情内容："+JSON.stringify(res));
    $scope.data = res;
    $scope.couponList = res.couponList;
    if($scope.couponList.length <= 0){
        $scope.hide_coupon_list = true;
    }else{
        $scope.hide_coupon_list = false;
    }
  })

  $scope.go_coupon_details = function(type,id){
      //x 券的类型 1 代金券 2礼品券   y 为券的id
      if(type == 1){//跳转到代金券；
        $state.go('tab.link_coupon',{id:id});
      }else if(type == 2){//跳转礼品券
        $state.go('tab.link_goods',{id:id});
      }
  }

})
//发现-》品牌店铺详情
.controller('f_shop_detailCtrl', function($scope,$state,$stateParams,$ionicHistory,homeModule){
  $scope.goback = function(){
    $ionicHistory.goBack();
  }
  //$stateParams 接收传递过来的参数；//   console.log("最新店铺列表传过来的id："+JSON.stringify($stateParams));
  $scope.shop_id = $stateParams.shop_id;
  // 查询店铺详情// getShopDetail// int shop_id
  homeModule.shop_detail("getShopDetail", $scope.shop_id ).then(function(res){
    $scope.data = res.shop;
    //  console.log("详情内容1："+JSON.stringify($scope.data));
    $scope.imageIp=res.imageIp;
    //  console.log("详情内容2："+JSON.stringify($scope.imageIp));
  })

})
//发现-》品牌店铺详情
.controller('f_shop_detailCtrl', function($scope,$state,$stateParams,$ionicHistory,homeModule){
  $scope.goback=function(){
    $ionicHistory.goBack();
  }
  //$stateParams 接收传递过来的参数；//   console.log("最新店铺列表传过来的id："+JSON.stringify($stateParams));
  $scope.shop_id = $stateParams.shop_id;
  // 查询店铺详情// getShopDetail// int shop_id
  homeModule.shop_detail("getShopDetail", $scope.shop_id ).then(function(res){
    $scope.data = res.shop;
    //  console.log("详情内容1："+JSON.stringify($scope.data));
    $scope.imageIp=res.imageIp;
    //  console.log("详情内容2："+JSON.stringify($scope.imageIp));
  })

})


// //发现-》活动详情
.controller('active_detailCtrl', function($scope,$state,$stateParams,$ionicHistory,homeModule){
  $scope.goback = function(){
    $ionicHistory.goBack();
  }
  
  //$stateParams 接收传递过来的参数；
//   console.log("最新活动列表传过来的id："+JSON.stringify($stateParams));
  $scope.active_id = $stateParams.active_id;
  // 查询活动详情
  // getActivitiesDetail
  // int act_id
  homeModule.active_detail("getActivitiesDetail",$scope.active_id ).then(function(res){
    console.log("huodong详情内容："+JSON.stringify(res));
    $scope.data = res;
    $scope.couponList = res.couponList;
    if($scope.couponList.length <= 0){
        $scope.hide_coupon_list = true;
    }else{
        $scope.hide_coupon_list = false;
    }
  })

  $scope.go_coupon_details = function(type,id){
      //x 券的类型 1 代金券 2礼品券   y 为券的id
      if(type == 1){//跳转到代金券；
        $state.go('tab.link_coupon',{id:id});
      }else if(type == 2){//跳转礼品券
        $state.go('tab.link_goods',{id:id});
      }
  }

})
//发现-》品牌店铺详情
.controller('f_shop_detailCtrl', function($scope,$state,$stateParams,$ionicHistory,homeModule){
  $scope.goback = function(){
    $ionicHistory.goBack();
  }
  //$stateParams 接收传递过来的参数；
//   console.log("最新店铺列表传过来的id："+JSON.stringify($stateParams));
  $scope.shop_id = $stateParams.shop_id;
  // 查询店铺详情
  // getShopDetail
  // int shop_id
  homeModule.shop_detail("getShopDetail", $scope.shop_id ).then(function(res){
    $scope.data = res.shop;
    //  console.log("详情内容1："+JSON.stringify($scope.data));
    $scope.imageIp=res.imageIp;
    //  console.log("详情内容2："+JSON.stringify($scope.imageIp));
  })

})
//商品详情goodsCtrl
.controller('link_goodsCtrl',function($ionicPopup,$state,Storage,Common,md5,$ionicLoading,$ionicModal,loginModule,$scope,$ionicTabsDelegate,$ionicHistory,$stateParams,mineModule){
  $scope.goback=function(){
    $ionicHistory.goBack();
  }
  //登录页面
  $ionicModal.fromTemplateUrl('templates/modal.html', {
      scope: $scope
  }).then(function(modal) {
      $scope.modal = modal;
  });
  //登录页面
  //获取传递过来的id
  $scope.good_id = $stateParams.id;
  console.log("礼品："+ $scope.good_id );

  //通过ID获取商品详情
  $scope.get_good_details = function(){
     var params = {
      goodsId:$scope.good_id
    }
    mineModule.http("getGoodDetail",params).then(function(res){
      // console.log("good:"+JSON.stringify(res));
      if(res.code = 10000){
        $scope.good_details = res.info;
        //格式化要显示的数据
        $scope.good_details.levels = $scope.good_details.levels.join(" ")
        if($scope.good_details.couponInfo.beginTime){
          $scope.good_details.couponInfo.beginTime = $scope.good_details.couponInfo.beginTime.split(" ")[0];
        }
        if($scope.good_details.couponInfo.endTime){
          $scope.good_details.couponInfo.endTime = $scope.good_details.couponInfo.endTime.split(" ")[0];
        }
        console.log("good详情："+JSON.stringify($scope.good_details));
      }else{
        // alert("获取数据出错了！")
      }

    })
  }
  $scope.get_good_details();
  //判断是否登录
  $scope.go_confirm = function(){
    if(Storage.ls_get("user_infos")){
      //登录过直接跳转确认订单页面
      $state.go("tab.get_confirm",{id:$scope.good_id});
    }else{
      $scope.modal.show();
      // alert("登录后才能购买！")
      //未登录
        //显示登录模块
          //登录成功后跳转确认订单页面
    }
  }

  
//new 登录注册模块
// 10200 //请求成功!  10201 // 验证码失效  10202 // 该手机尚未注册!  10203 // 手机号已经被注册  10204 //手机号或者密码错误!
    //登录方式切换
      //默认初始化显示快登陆
      $scope.pwd_login_2 = false;
      $scope.test_code_login_2 = true;
      $scope.show_M = 0;
    $scope.changeModel = function(n){
      $scope.show_M = n;
      if( n == 0){
         $scope.pwd_login_2 = false;
         $scope.test_code_login_2 = true;
      }else{
         $scope.pwd_login_2 = true;
         $scope.test_code_login_2 = false;
      }
    }
    //登录注册切换
    $scope.display_login = true;
    $scope.display_register = false;
    $scope.m_title = "登录"
    $scope.change_page = function(num){
      if(num == 0){
        $scope.display_login = true;
        $scope.display_register = false;
        $scope.m_title = "登录"
      }
      if(num == 1){
        $scope.display_login = false;
        $scope.display_register = true;
        $scope.m_title = "注册"
      }
      $scope.captcha = {
        paracont: '获取验证码',
        is_disable: false,
        paraevent: true
      };
    }

    //登录逻辑(快捷登录)
    //获取验证码
    $scope.user_info = {};
    $scope.captcha = {
      paracont: '获取验证码',
      is_disable: false,
      paraevent: true
    };
    $scope.get_test_code = function(e){
      var mobile =  $scope.user_info.user_num;
      var area = document.getElementById('fgood_DL_area').value;
      // alert(area);
      if ($scope.captcha.paraevent) {
        if (Common.checkMobile(mobile)) {
          // console.log(mobile);
          $ionicLoading.show({
            template: '获取验证码...'
          });
          //调取验证码服务；
          Common.countDown($scope.captcha);
          loginModule.get_captcha(mobile,area).then(function(res){
            $ionicLoading.hide();
            if(res.result == 10202){
              $scope.change_page(1)//切换到登录页面
              $cordovaToast.show('该手机尚未注册,注册后可快捷登录!', 'long', 'center');
              // alert("该手机尚未注册,注册后可快捷登录!");
            }
          })
        }else{
          //手机号码不对；
        }
      } else {
          e.preventDefault();
      }
    }
    // 快捷登录(登录按钮)
    $scope.LOGIN = function(){
      console.log("开始验证了+++++++++++++++++++++++++++++++++++++")
      var mobile =  $scope.user_info.user_num;
      var code = $scope.user_info.test_code;
      //正则及相关验证；
      var _rep = /\d{6}/.test(code);
      var _mobile = /^\d{5,}$/.test(mobile);
      if(mobile&&_mobile){//验证通过；
        $scope.phone_tc = " "
        if((_rep && code.length === 6)){//tongguo 
          $ionicLoading.show({
            template: '正在登录中...'
          });
          $scope.test_code_tc = " "
          loginModule.login(mobile,code).then(function(res){
            $ionicLoading.hide();
            console.log("快捷登录的结果："+JSON.stringify(res))
            switch(res.result){//10200 //请求成功!10201 // 验证码失效10202 // 该手机尚未注册!10203 // 手机号已经被注册10204 //手机号或者密码错误!
              case 10200:
                // alert("请求成功");
                //掉用成功的方法
                $scope.member_info = res.member //绑定会员相关数据
                console.log("会员相关信息："+ $scope.member_info);
                //$scope.top_info = true; //切换已登录的模板
                $scope.no_user_infos = false;
                // $scope.no_user_infos = false;//显示会员信息区域
                Storage.ls_set("user_infos",$scope.member_info);//将会员信息本地存储
                $scope.modal.hide();//将会员信息本地存储
                //$state.go("tab.confirm_order",{id:$scope.coupon_id});//跳转对应的优惠券详情页面//////////////////////////
                //隐藏登录入口
                $scope.no_login = false;
                //清除相关用户信息
                $scope.user_info.user_num = "";
                $scope.user_info.test_code = "";

                //记住密码（防止验证码登录后修改密码报错）
                $scope.id_pwd = {
                  user_num:mobile,
                  user_pwd:''
                };
                Storage.ls_set("user&pwd",$scope.id_pwd);//记住账号及密码

                $cordovaToast.show('登录成功!', 'long', 'center');
                break;
              case 10201:
                // alert("");
                $cordovaToast.show('验证码失效或错误!', 'long', 'center');
                break;
              case 10202:
                // alert("");
                $cordovaToast.show('该手机尚未注册', 'long', 'center');
                break;
              case 10203:
                // alert("");
                $cordovaToast.show('手机号已经被注册', 'long', 'center');
                break;
              case 10204:
                // alert("");
                $cordovaToast.show('手机号或者密码错误', 'long', 'center');
                break;
              default:
                // alert("");
                $cordovaToast.show('出错了，稍后再试', 'long', 'center');
            }
          })
        }else{//手机号错误
          $scope.test_code_tc = "请输入6位正确的验证码"
        }
      }else{//验证码未填或填错；
        $scope.phone_tc = "请输入正确的手机号码"
      }
    }
    //清除验证状态
    $scope.phone_test = function(){//手机号码状态清除
      $scope.phone_tc = " ";
      //账号密码登录状态
      $scope.reg_notice_phone = "";//手机号正则状态清除
      $scope.pwd_login_res = "";//登陆结果状态
    }
    $scope.verification_test = function(){//验证码状态清除
      $scope.test_code_tc = " ";
      //账号密码登录状态
      $scope.reg_notice_pwd = "";//pwd正则状态清除
      $scope.pwd_login_res = "";//登陆结果状态
    }

//账号密码登录
    $scope.n_user_info = {};
    $scope.login_pwd = function(){
      
      var p_user = $scope.n_user_info.user_num;
      var P_pwd = $scope.n_user_info.user_pwd;
      //正则验证；
      // var reg_pwd = /^[a-zA-Z]\w{5,17}$/;
      var reg_mobile = /^\d{5,}$/;

      if(reg_mobile.test(p_user)){//手机号验证通过
        if(P_pwd&&P_pwd.length>7){//密码正则通过
          $ionicLoading.show({
            template: '正在登录中...'
          });
          var md5_pwd = md5.createHash(P_pwd);
          console.log(md5_pwd);
          //进行登录
          loginModule.n_login(p_user,md5_pwd).then(function(res){
            $ionicLoading.hide();
            console.log("会员细信息："+JSON.stringify(res));
            switch(res.result){//10200 //请求成功!10201 // 验证码失效10202 // 该手机尚未注册!10203 // 手机号已经被注册10204 //手机号或者密码错误!
              case 10200:
                // alert("请求成功");
                //显示会员相关信息
                $scope.member_info = res.member;
                Storage.ls_set("user_infos",res.member);
                $scope.no_user_infos = false;//显示个人信息这块
                $scope.no_login = false//关闭登录入口
                //隐藏登录
                $scope.modal.hide();
                //$state.go("tab.confirm_order",{id:$scope.coupon_id});//跳转对应的优惠券详情页面//////////////////////////
                // 记住密码；
                if($scope.ischecked){
                  // alert("记住密码")
                  Storage.ls_set("user&pwd", $scope.n_user_info);//记住账号及密码
                }else{
                  Storage.ls_delete("user&pwd");//账号
                  // alert("删除密码")
                  $scope.n_user_info = {};//删除账号及密码
                }
                $cordovaToast.show('登录成功!', 'long', 'center');
                break;
              case 10201:
                // alert("验证码失效");
                $scope.pwd_login_res = "验证码失效或错误"
                break;
              case 10202:
                // alert("该手机尚未注册");
                $scope.pwd_login_res = "该手机尚未注册"
                break;
              case 10203:
                // alert("手机号已经被注册");
                $scope.pwd_login_res = "手机号已经被注册"
                break;
              case 10204:
                // alert("手机号或者密码错误");
                $scope.pwd_login_res = " 手机号或者密码错误"
                break;
              default:
                // alert("出错了，稍后再试");
                $scope.pwd_login_res = "出错了，稍后再试"
            }
           
          })
        }else{//密码正则未通过
           $scope.reg_notice_pwd = "数字和者字母（6到18位）"
        }
      }else{//手机号正则不通过
        $scope.reg_notice_phone = "手机号不正确"
      }

    }
  //获取或设置复选框是否选中
  $scope.ischecked = false;
  //点击切换显密码
  $scope.show_pwd_value = "显示"
  $scope.show_pwd = function(){
    var input = document.getElementById("pwd");
    if(input.type == "text"){
      console.log("pwd")
      input.type = "password";
      $scope.show_pwd_value = "显示"
    }else{
      console.log("text")
      input.type = "text";
      $scope.show_pwd_value = "隐藏"
    }

  }

   //注册模块
  $scope.register_info = {};//存放用户注册信息
  $scope.register = function(){
   
    //正则验证
    var r_mobile = $scope.register_info.phone
    var r_pwd = $scope.register_info.pwd;
    var r_code = $scope.register_info.code;
    var _mobile = /^\d{5,}$/.test(r_mobile);
    var _code = /\d{6}/.test(r_code);
    var area = document.getElementById('fgood_ZC_area').value;
    // alert(area);
    if(_mobile){
      if(r_pwd && r_pwd.length>7 ){
        if(_code){
          $ionicLoading.show({
            template: '正在注册中...'
          });
          var params = {
            m_phone : r_mobile,
            code : r_code ,
            m_pass : md5.createHash( r_pwd ),
            area:area
          };
          loginModule.register(params).then(function(res){
            console.log("注册结果"+JSON.stringify(res));
            $ionicLoading.hide();
            switch(res.result){
              case 10200:
                // alert("注册成功,马上登录");
                $scope.change_page(0);//切换至登录模块
                $cordovaToast.show('注册成功,马上登录!', 'long', 'center');
                break;
              case 10201:
                $scope.r_pwd_login_res_code = "验证码失效或错误";
                break;
              case 10202:
                $scope.r_pwd_login_res = "该手机尚未注册";
                break;
              case 10203:
                $scope.r_pwd_login_res = "手机号已经被注册";
                break;
              case 10204:
                $scope.r_pwd_login_res = " 手机号或者密码错误";
                break;
              default:
                $scope.r_pwd_login_res = "出错了，稍后再试";
            }
          })
        }else{
          $ionicPopup.alert({
            title: '提示',
            template: '请输入6位数字验证码！'
          });
        }
      }else{
        // alert("密码为8位以上数字或字母")
        $scope.r_pwd_num = "密码为8位以上数字或字母"
      }
    }else{
      // alert("手机号码不正确")
      $scope.r_phone_num = "手机号码不正确"
    }
   
  }
  //清除注册提示状态
  $scope.hide_notice = function(){
    $scope.r_phone_num = "";
    $scope.r_pwd_num = "";
    $scope.r_pwd_login_res_code = "";
    $scope.r_pwd_login_res = "";
  }
  // 验证码(注册模块)
  $scope.register_code = function(e){
    var phone =  $scope.register_info.phone;
    var area = document.getElementById('fgood_ZC_area').value;
    // alert(area);
    // alert(phone);
    if ($scope.captcha.paraevent) {
      if (Common.checkMobile(phone)){
        $ionicLoading.show({
          template: '获取验证码...'
        });
        //调取验证码服务；
        loginModule.reg_captcha(phone,area).then(function(res){
          $ionicLoading.hide();
          console.log(res);
          if(res.result == "10203"){
            $scope.r_pwd_login_res = "手机号已经注册，请直接登录";
          }else{
            Common.countDown($scope.captcha);
          }
        })
      }else{
        //手机号码不对；
      }
    } else {
        e.preventDefault();
    }
  }
  //重置验证码提示消息
  $scope.set_code_notice = function(){
     $scope.r_pwd_login_res = " "
  }
  //切换密码
  $scope.ischecked = true;//初始化为记住密码状态
  $scope.toggle_pwd = function(){
    if($scope.ischecked){
      $scope.ischecked = false;
    }else{
      $scope.ischecked = true;
    }
  }

    
})

//优惠券详情
.controller('link_couponCtrl',function($ionicPopup,Common,md5,$ionicLoading,$ionicModal,loginModule,$scope,$state,Storage,$ionicTabsDelegate,$ionicHistory,$stateParams,mineModule){
  $scope.goback=function(){
    $ionicHistory.goBack();
  }

  //登录页面
  $ionicModal.fromTemplateUrl('templates/modal.html', {
      scope: $scope
  }).then(function(modal) {
      $scope.modal = modal;
  });
  //登录页面

  $scope.coupon_id = $stateParams.id;
  // console.log("优惠券："+ $scope.coupon_id );
  //获取优惠券信息详情；
  $scope.get_coupons_details = function(){
    var params = {
      goodsId:$scope.coupon_id
    }
    mineModule.http("getGoodDetail",params).then(function(res){
      console.log("优惠券详情："+JSON.stringify(res))
      if( res.code == 10000 ){
        $scope.coupons_details = res.info
        $scope.coupons_details.levels = $scope.coupons_details.levels.join(" ");
        if( $scope.coupons_details.couponInfo.beginTime){
           $scope.coupons_details.couponInfo.beginTime = $scope.coupons_details.couponInfo.beginTime.split(" ")[0];
        }
        if($scope.coupons_details.couponInfo.endTime){
           $scope.coupons_details.couponInfo.endTime = $scope.coupons_details.couponInfo.endTime.split(" ")[0];
        }
      }else{
        // alert("获取数据失败");
      }
    })
  }
  $scope.get_coupons_details();

  $scope.go_confirm = function(){
    if(Storage.ls_get("user_infos")){
      //登录过直接跳转确认订单页面
      $state.go("tab.get_confirm",{id:$scope.coupon_id});
    }else{
      $scope.modal.show();
      // alert("登录后才能购买！")
      //未登录
        //显示登录模块
          //登录成功后跳转确认订单页面
    }
  }


//new 登录注册模块
// 10200 //请求成功!  10201 // 验证码失效  10202 // 该手机尚未注册!  10203 // 手机号已经被注册  10204 //手机号或者密码错误!
    //登录方式切换
      //默认初始化显示快登陆
      $scope.pwd_login_2 = false;
      $scope.test_code_login_2 = true;
      $scope.show_M = 0;
    $scope.changeModel = function(n){
      $scope.show_M = n;
      if( n == 0){
         $scope.pwd_login_2 = false;
         $scope.test_code_login_2 = true;
      }else{
         $scope.pwd_login_2 = true;
         $scope.test_code_login_2 = false;
      }
    }
    //登录注册切换
    $scope.display_login = true;
    $scope.display_register = false;
    $scope.m_title = "登录"
    $scope.change_page = function(num){
      if(num == 0){
        $scope.display_login = true;
        $scope.display_register = false;
        $scope.m_title = "登录"
      }
      if(num == 1){
        $scope.display_login = false;
        $scope.display_register = true;
        $scope.m_title = "注册"
      }
      $scope.captcha = {
        paracont: '获取验证码',
        is_disable: false,
        paraevent: true
      };
    }

    //登录逻辑(快捷登录)
    //获取验证码
    $scope.user_info = {};
    $scope.captcha = {
      paracont: '获取验证码',
      is_disable: false,
      paraevent: true
    };
    $scope.get_test_code = function(e){
      var mobile =  $scope.user_info.user_num;
      var area = document.getElementById('fcoupon_DL_area').value;
    // alert(area);
      if ($scope.captcha.paraevent) {
        if (Common.checkMobile(mobile)) {
          // console.log(mobile);
          $ionicLoading.show({
            template: '获取验证码...'
          });
          //调取验证码服务；
          Common.countDown($scope.captcha);
          loginModule.get_captcha(mobile,area).then(function(res){
            $ionicLoading.hide();
            if(res.result == 10202){
              $scope.change_page(1)//切换到登录页面
              $cordovaToast.show('该手机尚未注册,注册后可快捷登录!', 'long', 'center');
              // alert("该手机尚未注册,注册后可快捷登录!");
            }
          })
        }else{
          //手机号码不对；
        }
      } else {
          e.preventDefault();
      }
    }
    // 快捷登录(登录按钮)
    $scope.LOGIN = function(){
      console.log("开始验证了+++++++++++++++++++++++++++++++++++++")
      var mobile =  $scope.user_info.user_num;
      var code = $scope.user_info.test_code;
      //正则及相关验证；
      var _rep = /\d{6}/.test(code);
      var _mobile = /^\d{5,}$/.test(mobile);
      if(mobile&&_mobile){//验证通过；
        $scope.phone_tc = " "
        if((_rep && code.length === 6)){//tongguo 
          $ionicLoading.show({
            template: '正在登录中...'
          });
          $scope.test_code_tc = " "
          loginModule.login(mobile,code).then(function(res){
            $ionicLoading.hide();
            console.log("快捷登录的结果："+JSON.stringify(res))
            switch(res.result){//10200 //请求成功!10201 // 验证码失效10202 // 该手机尚未注册!10203 // 手机号已经被注册10204 //手机号或者密码错误!
              case 10200:
                // alert("请求成功");
                //掉用成功的方法
                $scope.member_info = res.member //绑定会员相关数据
                console.log("会员相关信息："+ $scope.member_info);
                //$scope.top_info = true; //切换已登录的模板
                $scope.no_user_infos = false;
                // $scope.no_user_infos = false;//显示会员信息区域
                Storage.ls_set("user_infos",$scope.member_info);//将会员信息本地存储
                $scope.modal.hide();//将会员信息本地存储
                //$state.go("tab.confirm_order",{id:$scope.coupon_id});//跳转对应的优惠券详情页面//////////////////////////
                //隐藏登录入口
                $scope.no_login = false;
                //清除相关用户信息
                $scope.user_info.user_num = "";
                $scope.user_info.test_code = "";

                //记住密码（防止验证码登录后修改密码报错）
                $scope.id_pwd = {
                  user_num:mobile,
                  user_pwd:''
                };
                Storage.ls_set("user&pwd",$scope.id_pwd);//记住账号及密码

                $cordovaToast.show('登录成功!', 'long', 'center');
                break;
              case 10201:
                // alert("");
                $cordovaToast.show('验证码失效或错误!', 'long', 'center');
                break;
              case 10202:
                // alert("");
                $cordovaToast.show('该手机尚未注册', 'long', 'center');
                break;
              case 10203:
                // alert("");
                $cordovaToast.show('手机号已经被注册', 'long', 'center');
                break;
              case 10204:
                // alert("");
                $cordovaToast.show('手机号或者密码错误', 'long', 'center');
                break;
              default:
                // alert("");
                $cordovaToast.show('出错了，稍后再试', 'long', 'center');
            }
          })
        }else{//手机号错误
          $scope.test_code_tc = "请输入6位正确的验证码"
        }
      }else{//验证码未填或填错；
        $scope.phone_tc = "请输入正确的手机号码"
      }
    }
    //清除验证状态
    $scope.phone_test = function(){//手机号码状态清除
      $scope.phone_tc = " ";
      //账号密码登录状态
      $scope.reg_notice_phone = "";//手机号正则状态清除
      $scope.pwd_login_res = "";//登陆结果状态
    }
    $scope.verification_test = function(){//验证码状态清除
      $scope.test_code_tc = " ";
      //账号密码登录状态
      $scope.reg_notice_pwd = "";//pwd正则状态清除
      $scope.pwd_login_res = "";//登陆结果状态
    }

//账号密码登录
    $scope.n_user_info = {};
    $scope.login_pwd = function(){
      
      var p_user = $scope.n_user_info.user_num;
      var P_pwd = $scope.n_user_info.user_pwd;
      //正则验证；
      // var reg_pwd = /^[a-zA-Z]\w{5,17}$/;
      var reg_mobile = /^\d{5,}$/;

      if(reg_mobile.test(p_user)){//手机号验证通过
        if(P_pwd&&P_pwd.length>7){//密码正则通过
          $ionicLoading.show({
            template: '正在登录中...'
          });
          var md5_pwd = md5.createHash(P_pwd);
          console.log(md5_pwd);
          //进行登录
          loginModule.n_login(p_user,md5_pwd).then(function(res){
            $ionicLoading.hide();
            console.log("会员细信息："+JSON.stringify(res));
            switch(res.result){//10200 //请求成功!10201 // 验证码失效10202 // 该手机尚未注册!10203 // 手机号已经被注册10204 //手机号或者密码错误!
              case 10200:
                // alert("请求成功");
                //显示会员相关信息
                $scope.member_info = res.member;
                Storage.ls_set("user_infos",res.member);
                $scope.no_user_infos = false;//显示个人信息这块
                $scope.no_login = false//关闭登录入口
                //隐藏登录
                $scope.modal.hide();
                //$state.go("tab.confirm_order",{id:$scope.coupon_id});//跳转对应的优惠券详情页面//////////////////////////
                // 记住密码；
                if($scope.ischecked){
                  // alert("记住密码")
                  Storage.ls_set("user&pwd", $scope.n_user_info);//记住账号及密码
                }else{
                  Storage.ls_delete("user&pwd");//账号
                  // alert("删除密码")
                  $scope.n_user_info = {};//删除账号及密码
                }
                $cordovaToast.show('登录成功!', 'long', 'center');
                break;
              case 10201:
                // alert("验证码失效");
                $scope.pwd_login_res = "验证码失效或错误"
                break;
              case 10202:
                // alert("该手机尚未注册");
                $scope.pwd_login_res = "该手机尚未注册"
                break;
              case 10203:
                // alert("手机号已经被注册");
                $scope.pwd_login_res = "手机号已经被注册"
                break;
              case 10204:
                // alert("手机号或者密码错误");
                $scope.pwd_login_res = " 手机号或者密码错误"
                break;
              default:
                // alert("出错了，稍后再试");
                $scope.pwd_login_res = "出错了，稍后再试"
            }
           
          })
        }else{//密码正则未通过
           $scope.reg_notice_pwd = "数字和者字母（6到18位）"
        }
      }else{//手机号正则不通过
        $scope.reg_notice_phone = "手机号不正确"
      }

    }
  //获取或设置复选框是否选中
  $scope.ischecked = false;
  //点击切换显密码
  $scope.show_pwd_value = "显示"
  $scope.show_pwd = function(){
    var input = document.getElementById("pwd");
    if(input.type == "text"){
      console.log("pwd")
      input.type = "password";
      $scope.show_pwd_value = "显示"
    }else{
      console.log("text")
      input.type = "text";
      $scope.show_pwd_value = "隐藏"
    }

  }

   //注册模块
  $scope.register_info = {};//存放用户注册信息
  $scope.register = function(){
   
    //正则验证
    var r_mobile = $scope.register_info.phone
    var r_pwd = $scope.register_info.pwd;
    var r_code = $scope.register_info.code;
    var _mobile = /^\d{5,}$/.test(r_mobile);
    var _code = /\d{6}/.test(r_code);
    var area = document.getElementById('fcoupon_ZC_area').value;
    // alert(area);
    if(_mobile){
      if(r_pwd && r_pwd.length>7 ){
        if(_code){
          $ionicLoading.show({
            template: '正在注册中...'
          });
          var params = {
            m_phone : r_mobile,
            code : r_code ,
            m_pass : md5.createHash( r_pwd ),
            area:area
          };
          loginModule.register(params).then(function(res){
            console.log("注册结果"+JSON.stringify(res));
            $ionicLoading.hide();
            switch(res.result){
              case 10200:
                // alert("注册成功,马上登录");
                $scope.change_page(0);//切换至登录模块
                $cordovaToast.show('注册成功,马上登录!', 'long', 'center');
                break;
              case 10201:
                $scope.r_pwd_login_res_code = "验证码失效或错误";
                break;
              case 10202:
                $scope.r_pwd_login_res = "该手机尚未注册";
                break;
              case 10203:
                $scope.r_pwd_login_res = "手机号已经被注册";
                break;
              case 10204:
                $scope.r_pwd_login_res = " 手机号或者密码错误";
                break;
              default:
                $scope.r_pwd_login_res = "出错了，稍后再试";
            }
          })
        }else{
          $ionicPopup.alert({
            title: '提示',
            template: '请输入6位数字验证码！'
          });
        }
      }else{
        // alert("密码为8位以上数字或字母")
        $scope.r_pwd_num = "密码为8位以上数字或字母"
      }
    }else{
      // alert("手机号码不正确")
      $scope.r_phone_num = "手机号码不正确"
    }
   
  }
  //清除注册提示状态
  $scope.hide_notice = function(){
    $scope.r_phone_num = "";
    $scope.r_pwd_num = "";
    $scope.r_pwd_login_res_code = "";
    $scope.r_pwd_login_res = "";
  }
  // 验证码(注册模块)
  $scope.register_code = function(e){
    var phone =  $scope.register_info.phone;
    var area = document.getElementById('fcoupon_ZC_area').value;
    // alert(area);
    // alert(phone);
    if ($scope.captcha.paraevent) {
      if (Common.checkMobile(phone)){
        $ionicLoading.show({
          template: '获取验证码...'
        });
        //调取验证码服务；
        loginModule.reg_captcha(phone,area).then(function(res){
          $ionicLoading.hide();
          console.log(res);
          if(res.result == "10203"){
            $scope.r_pwd_login_res = "手机号已经注册，请直接登录";
          }else{
            Common.countDown($scope.captcha);
          }
        })
      }else{
        //手机号码不对；
      }
    } else {
        e.preventDefault();
    }
  }
  //重置验证码提示消息
  $scope.set_code_notice = function(){
     $scope.r_pwd_login_res = " "
  }
  //切换密码
  $scope.ischecked = true;//初始化为记住密码状态
  $scope.toggle_pwd = function(){
    if($scope.ischecked){
      $scope.ischecked = false;
    }else{
      $scope.ischecked = true;
    }
  }

})
//确认订单
.controller('get_confirmCtrl', function($cordovaToast,$stateParams,$scope,$ionicHistory,$cordovaToast,Storage,$ionicLoading,$state,mineModule){
  $scope.goback=function(){
    $ionicHistory.goBack();
  }
   $scope.coupon_id = $stateParams.id;
  //  console.log("确认订单页面："+ $scope.coupon_id );
  //通过ID获取详情
  $scope.get_order = function(){
     var params = {
      goodsId:$scope.coupon_id
    }
    mineModule.http("getGoodDetail",params).then(function(res){
      // console.log("确认订单："+JSON.stringify(res))
      if( res.code == 10000 ){
        $scope.coupons_details = res.info;
        $scope.levelsNo = res.info.levelsNo;//商品的等级
        console.log("商品等级："+$scope.levelsNo);
        //商品数量的加减
			  $scope.max_count = $scope.coupons_details.memberSaleMaxNum;//限购的最大值
			  $scope.good_count = 1;//订单至少购买一件
			  $scope.price = $scope.coupons_details.payMoney//后台获取(单价)
			  $scope.inte = $scope.coupons_details.payPoint//后台获取（单个需要的积分）
			  //初始化显示计算金额（按一份来算）
			  $scope.Total_price = $scope.price;
			  $scope.Total_inte = $scope.inte*$scope.good_count ;
        
      }else{
        $cordovaToast.show('获取数据出错了 '+res.code, 'long', 'center');
      }
    })
  }
  $scope.info = function(){
  	if(Storage.ls_get("user_infos")){
			var params = {
				memberUUID:JSON.parse(Storage.ls_get("user_infos")).memberuuid
	    }
	    mineModule.http("getMember",params).then(function(res){
	    	if(res.code = 10000){
          $scope.info = res.info;
          $scope.card_name = res.info.levelNo;//获取会员当前的会员等级
          console.log("当前的会员等级："+$scope.card_name);
	    		// console.log("会员的积分信息："+JSON.stringify(res));
	    		$scope.all_inte =  res.info.usableBonus //后台获取（会员当前的总积分 ，购买商品的积分不能大于总积分）
	    	}else{
          $cordovaToast.show('获取数据出错了 '+res.code, 'long', 'center');
	    	}
	    
	    })
		}else{
      $cordovaToast.show('暂时未获取到会员信息，请重新登录再试', 'long', 'center');
		}
  }
  //获取订单详情；
  $scope.get_order() 
  //获取会员积分
	$scope.info();

  $scope.add = function(){
    if($scope.max_count > $scope.good_count){
    	$scope.good_count++;
	  	$scope.Total_price = ($scope.price*$scope.good_count).toFixed(2);
	    $scope.Total_inte = $scope.inte*$scope.good_count;
    }else{
			$cordovaToast.show('不能超过最大限购数!', 'long', 'center')
    }
  }
  $scope.reduce = function(){
    if( $scope.good_count > 1 ){
      $scope.good_count--;
      $scope.Total_price = ($scope.price*$scope.good_count).toFixed(2);
      $scope.Total_inte = $scope.inte*$scope.good_count ;
    }else{
			$cordovaToast.show('至少兑换一件该商品!', 'long', 'center')
    }
  }

    //提交订单去支付
  $scope.go_pay = function(){
    //遮罩层；
    $ionicLoading.show({
      template: '努力获取订单中'
    });
    var h_AppTimer = setTimeout(function(){//超时30秒自动关闭遮罩层；
      $ionicLoading.hide();
      clearTimeout(h_AppTimer);
    },30000);
    //先判断等级是否匹配；
    if( $scope.levelsNo.indexOf($scope.card_name) != -1 ){
      //先判断会员积分是否够用
      if( $scope.all_inte >= $scope.Total_inte ){//积分够用
        var obj = document.getElementsByName("pay")
        for(var i=0; i<obj.length; i ++){
          if(obj[i].checked){
            $scope.pay_value = obj[i].value;
          }
        }
        var order_params={
          "trade_name":$scope.coupons_details.couponInfo.couponName,//订单名字
          "trade_num":$scope.good_count,//订单数量
          "memberUUID":JSON.parse(Storage.ls_get("user_infos")).memberuuid,
          "member_id":JSON.parse(Storage.ls_get("user_infos")).m_id,
          "member_card":$scope.info.cardNo,
          "member_phone": $scope.info.mobile,
          "total_amount":parseFloat($scope.Total_price) ,   /////////////////////   记得后面的重新支付也要改（控制器：pay_resCtrl）；
          "total_point": $scope.Total_inte,
          "total_fee":parseFloat($scope.Total_price)*100,   ////////////////////////////  记得后面的重新支付也要改（控制器：pay_resCtrl）；
          "tradeDetails": [{
            "goodsid": $scope.coupon_id,
            "goodsnum": $scope.good_count,
            "goodsprice": $scope.price,
            "goodspoint": $scope.Total_inte
          }],
          "goodsList":[{
            "goodsId": $scope.coupon_id,
            "goodsName":$scope.coupons_details.couponInfo.couponName ,
            "price": $scope.price,
            "point": $scope.Total_inte,
            "goodsCount":$scope.good_count,
          }]
        }
        if( $scope.pay_value == "1"){//支付宝
  //			alert(JSON.stringify(order_params));
          mineModule.pay("aliPay",order_params).then(function(res){
            $ionicLoading.hide();//关闭遮罩层
            console.log("返回的订单信息："+JSON.stringify(res));
            if(res.code == 10200){//创建订单成功
              //区分0元支付   实际金额支付；
              var cc = res.out_trade_no;
              if(res.order){
                var html = res.order;
                cordova.plugins.alipay.payment(html,function(success){
                  $state.go("tab.get_res",{id:"succ"+'.'+cc})
                }, function(error){
                  $cordovaToast.show(JSON.stringify(error), 'long', 'center');
                  // $state.go("tab.h_pay_res",{id:"erre"+'.'+html});//err
                  // Storage.ls_set("HTML",html);
                });
              }else{
                $ionicLoading.hide();
                $cordovaToast.show('购买成功~', 'long', 'center').then(function(succ){
                  $state.go("tab.get_res",{id:"succ"+'.'+cc});
                });
              }
            }else if(res.code == 10403){// 超出最大限购
              $ionicLoading.hide();
              $cordovaToast.show('创建订单失败,您已经累计超过最大限购~', 'long', 'center');
            }else{//创建订单失败
              $ionicLoading.hide();
              $cordovaToast.show('创建订单失败!('+res.code+')', 'long', 'center')
            }
          
          })
        }else if( $scope.pay_value == "2" ){//微信
          // alert(2)
          mineModule.pay("wxPayAPP",order_params).then(function(res){
            $ionicLoading.hide();
            console.log("微信支付相关参数：" + JSON.stringify( res ) )
            if(res.code == 10200){//创建订单成功
              //区分0元支付   实际金额支付；
              var cc = res.out_trade_no;
              if(res.app_params){//唤起微信支付；
                var params ={
                  appid:res.app_params.appid, //appid
                  package :res.app_params.package,//Sign=WXPay
                  partnerid:res.app_params.partnerid, // merchant id
                  prepayid:res.app_params.prepayid, // prepay id
                  noncestr:res.app_params.noncestr, // nonce
                  timestamp:res.app_params.timestamp, // timestamp
                  sign:res.app_params.sign // signed string
                };
                  
                Wechat.sendPaymentRequest(params, function () {
                  $state.go("tab.get_res",{id:"succ"+'.'+cc});
                }, function (reason) {
                  // $state.go("tab.h_pay_res",{id:"erre"+'.'+params});//err
                  // Storage.ls_set("WX_params",params);
                  $cordovaToast.show(reason, 'long', 'center');
                });
                
              }else{//0 元支付；
                $ionicLoading.hide();
                $cordovaToast.show('购买成功~', 'long', 'center').then(function(succ){
                  $state.go("tab.get_res",{id:"succ"+'.'+cc});
                });
              }
            }else if(res.code == 10403){// 超出最大限购
              $ionicLoading.hide();
              $cordovaToast.show('创建订单失败,您已经累计超过最大限购~', 'long', 'center');
            }else{//创建订单失败
              $ionicLoading.hide();
              $cordovaToast.show('创建订单失败!('+res.code+')', 'long', 'center');
            }
          
          })

        }else{//既不是支付宝也不是微信
          $ionicLoading.hide();
          return;
        }
        
      }else{//积分不够用
        $ionicLoading.hide();
        $cordovaToast.show('您的积分不够!', 'long', 'center');
      }

    }else{
      $ionicLoading.hide(); 
      $cordovaToast.show('您的会员等级暂时不能兑换该商品~', 'long', 'center');
    }

  	
  }
  
})